| Conditions | 1 |
| Paths | 6 |
| Total Lines | 257 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 0 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | // make sure ajaxurl is defined |
||
| 7 | jQuery(function($) { |
||
| 8 | var valid = false; |
||
| 9 | $('#wpinv_checkout_form').on('submit', function(e) { |
||
| 10 | var $form = $(this).closest('#wpinv_checkout_form'); |
||
| 11 | $('.wpinv_errors').remove(); |
||
| 12 | if (valid) { |
||
| 13 | return true; |
||
| 14 | } |
||
| 15 | e.preventDefault(); |
||
| 16 | wpinvBlock($form); |
||
| 17 | var data = $form.serialize(); |
||
| 18 | data = wpinvRemoveQueryVar(data, 'action'); |
||
| 19 | data = wpinvRemoveQueryVar(data, 'wpinv_ajax'); |
||
| 20 | $.post(ajaxurl, data + '&action=wpinv_checkout', function(res) { |
||
| 21 | if (res && typeof res == 'object' && res.success) { |
||
| 22 | valid = true; |
||
| 23 | var data = new Object(); |
||
| 24 | data.form = $form; |
||
| 25 | data.totals = res.data; |
||
| 26 | jQuery('body').trigger('wpinv_checkout_submit', data); |
||
| 27 | if (window.wpiSubmit) { |
||
| 28 | $form.submit(); |
||
| 29 | } |
||
| 30 | } else { |
||
| 31 | $form.unblock(); |
||
| 32 | if (res && res.search("wpinv_adddress_confirm") !== -1) { |
||
| 33 | $('#wpinv_adddress_confirm').show(); |
||
| 34 | } |
||
| 35 | $('#wpinv_purchase_submit', $form).before(res); |
||
| 36 | } |
||
| 37 | }); |
||
| 38 | return false; |
||
| 39 | }); |
||
| 40 | var elB = $('#wpinv-fields'); |
||
| 41 | $('#wpinv_country', elB).change(function(e) { |
||
| 42 | $('.wpinv_errors').remove(); |
||
| 43 | wpinvBlock(jQuery('#wpinv_state_box')); |
||
| 44 | var $this = $(this); |
||
| 45 | data = { |
||
| 46 | action: 'wpinv_get_states_field', |
||
| 47 | country: $(this).val(), |
||
| 48 | field_name: 'wpinv_state', |
||
| 49 | }; |
||
| 50 | $.post(ajaxurl, data, function(response) { |
||
| 51 | if ('nostates' === response) { |
||
| 52 | var text_field = '<input type="text" required="required" class="wpi-input required" id="wpinv_state" name="wpinv_state">'; |
||
| 53 | $('#wpinv_state', elB).replaceWith(text_field); |
||
| 54 | } else { |
||
| 55 | $('#wpinv_state', elB).replaceWith(response); |
||
| 56 | var changeState = function() { |
||
| 57 | console.log('69 : wpinv_recalculate_taxes(' + $(this).val() + ')'); |
||
| 58 | wpinv_recalculate_taxes($(this).val()); |
||
| 59 | }; |
||
| 60 | $("#wpinv_state").unbind("change", changeState); |
||
| 61 | $("#wpinv_state").bind("change", changeState); |
||
| 62 | } |
||
| 63 | $('#wpinv_state', elB).find('option[value=""]').remove(); |
||
| 64 | $('#wpinv_state', elB).addClass('form-control wpi-input required'); |
||
| 65 | }).done(function(data) { |
||
| 66 | jQuery('#wpinv_state_box').unblock(); |
||
| 67 | console.log('78 : wpinv_recalculate_taxes()'); |
||
| 68 | wpinv_recalculate_taxes(); |
||
| 69 | }); |
||
| 70 | return false; |
||
| 71 | }); |
||
| 72 | $('select#wpinv_state', elB).change(function(e) { |
||
| 73 | $('.wpinv_errors').remove(); |
||
| 74 | console.log('86 : wpinv_recalculate_taxes()'); |
||
| 75 | wpinv_recalculate_taxes($(this).val()); |
||
| 76 | }); |
||
| 77 | var WPInv_Checkout = { |
||
| 78 | checkout_form: $('form#wpinv_checkout_form'), |
||
| 79 | init: function() { |
||
| 80 | if (!$(this.checkout_form).length) { |
||
| 81 | return; |
||
| 82 | } |
||
| 83 | // Payment methods |
||
| 84 | this.checkout_form.on('click', 'input[name="wpi-gateway"]', this.payment_method_selected); |
||
| 85 | this.init_payment_methods(); |
||
| 86 | //this.recalculate_taxes(); |
||
| 87 | }, |
||
| 88 | init_payment_methods: function() { |
||
| 89 | var $checkout_form = this.checkout_form; |
||
| 90 | var $payment_methods = $('.wpi-payment_methods input[name="wpi-gateway"]'); |
||
| 91 | // If there is one method, we can hide the radio input |
||
| 92 | if (1 === $payment_methods.length) { |
||
| 93 | $payment_methods.eq(0).hide(); |
||
| 94 | } |
||
| 95 | // If there are none selected, select the first. |
||
| 96 | if (0 === $payment_methods.filter(':checked').length) { |
||
| 97 | $payment_methods.eq(0).prop('checked', true); |
||
| 98 | } |
||
| 99 | // Trigger click event for selected method |
||
| 100 | $payment_methods.filter(':checked').eq(0).trigger('click'); |
||
| 101 | // Validate and apply a discount |
||
| 102 | $checkout_form.on('click', '#wpi-apply-discount', this.applyDiscount); |
||
| 103 | // Prevent the checkout form from submitting when hitting enter key in the discount field |
||
| 104 | $checkout_form.on('keypress', '#wpinv_discount_code', function(event) { |
||
| 105 | if (event.keyCode == '13') { |
||
| 106 | return false; |
||
| 107 | } |
||
| 108 | }); |
||
| 109 | // Apply the discount when hitting enter key in the discount field instead |
||
| 110 | $checkout_form.on('keyup', '#wpinv_discount_code', function(event) { |
||
| 111 | if (event.keyCode == '13') { |
||
| 112 | $('#wpi-apply-discount', $checkout_form).trigger('click'); |
||
| 113 | } |
||
| 114 | }); |
||
| 115 | // Remove a discount |
||
| 116 | $(document.body).on('click', '.wpi-discount-remove', this.removeDiscount); |
||
| 117 | }, |
||
| 118 | payment_method_selected: function() { |
||
| 119 | if ($('.wpi-payment_methods input.wpi-pmethod').length > 1) { |
||
| 120 | var target_payment_box = $('div.payment_box.' + $(this).attr('ID')); |
||
| 121 | if ($(this).is(':checked') && !target_payment_box.is(':visible')) { |
||
| 122 | $('div.payment_box').filter(':visible').slideUp(250); |
||
| 123 | if ($(this).is(':checked')) { |
||
| 124 | var content = $('div.payment_box.' + $(this).attr('ID')).html(); |
||
| 125 | content = content ? content.trim() : ''; |
||
| 126 | if (content) { |
||
| 127 | $('div.payment_box.' + $(this).attr('ID')).slideDown(250); |
||
| 128 | } |
||
| 129 | } |
||
| 130 | } |
||
| 131 | } else { |
||
| 132 | $('div.payment_box').show(); |
||
| 133 | } |
||
| 134 | $('#wpinv_payment_mode_select').attr('data-gateway', $(this).val()); |
||
| 135 | wpinvSetPaymentBtnText($(this), $('#wpinv_payment_mode_select').data('free')); |
||
| 136 | }, |
||
| 137 | applyDiscount: function(e) { |
||
| 138 | e.preventDefault(); |
||
| 139 | var $this = $(this), |
||
| 140 | $box = $this.closest('.panel-body'), |
||
| 141 | discount_code = $('#wpinv_discount_code', $box).val(), |
||
| 142 | $msg = $('.wpinv-discount-msg', $box), |
||
| 143 | $msgS = $('.alert-success', $msg), |
||
| 144 | $msgF = $('.alert-error', $msg); |
||
| 145 | if (discount_code == '') { |
||
| 146 | $('#wpinv_discount_code', $box).focus(); |
||
| 147 | return false; |
||
| 148 | } |
||
| 149 | var data = { |
||
| 150 | action: 'wpinv_apply_discount', |
||
| 151 | code: discount_code, |
||
| 152 | _nonce: WPInv.nonce |
||
| 153 | }; |
||
| 154 | $('.wpinv_errors').remove(); |
||
| 155 | $msg.hide(); |
||
| 156 | $msgS.hide().find('.wpi-msg').html(''); |
||
| 157 | $msgF.hide().find('.wpi-msg').html(''); |
||
| 158 | wpinvBlock($box); |
||
| 159 | $.ajax({ |
||
| 160 | type: "POST", |
||
| 161 | data: data, |
||
| 162 | dataType: "json", |
||
| 163 | url: WPInv.ajax_url, |
||
| 164 | xhrFields: { |
||
| 165 | withCredentials: true |
||
| 166 | }, |
||
| 167 | success: function(res) { |
||
| 168 | wpinvUnblock($box); |
||
| 169 | var success = false; |
||
| 170 | if (res && typeof res == 'object') { |
||
| 171 | if (res.success) { |
||
| 172 | success = true; |
||
| 173 | jQuery('#wpinv_checkout_cart_form', $this.checkout_form).replaceWith(res.data.html); |
||
| 174 | jQuery('.wpinv-chdeckout-total').text(res.data.total); |
||
| 175 | $('#wpinv_discount_code', $box).val(''); |
||
| 176 | //console.log('217 : wpinv_recalculate_taxes()'); |
||
| 177 | //wpinv_recalculate_taxes(); |
||
| 178 | if (res.data.free) { |
||
| 179 | $('#wpinv_payment_mode_select', $this.checkout_form).hide(); |
||
| 180 | gw = 'manual'; |
||
| 181 | } else { |
||
| 182 | $('#wpinv_payment_mode_select', $this.checkout_form).show(); |
||
| 183 | gw = $('#wpinv_payment_mode_select', $this.checkout_form).attr('data-gateway'); |
||
| 184 | } |
||
| 185 | $('.wpi-payment_methods .wpi-pmethod[value="' + gw + '"]', $this.checkout_form).prop('checked', true); |
||
| 186 | wpinvSetPaymentBtnText($('.wpi-payment_methods .wpi-pmethod[value="' + gw + '"]', $this.checkout_form), res.data.free); |
||
| 187 | $(document.body).trigger('wpinv_discount_applied', [res]); |
||
| 188 | } |
||
| 189 | if (res.msg) { |
||
| 190 | $msg.show(); |
||
| 191 | if (success) { |
||
| 192 | $msgS.show().find('.wpi-msg').html(res.msg); |
||
| 193 | } else { |
||
| 194 | $msgF.show().find('.wpi-msg').html(res.msg); |
||
| 195 | } |
||
| 196 | } |
||
| 197 | } |
||
| 198 | } |
||
| 199 | }).fail(function(res) { |
||
| 200 | wpinvUnblock($box); |
||
| 201 | if (window.console && window.console.log) { |
||
| 202 | console.log(res); |
||
| 203 | } |
||
| 204 | }); |
||
| 205 | return false; |
||
| 206 | }, |
||
| 207 | removeDiscount: function(e) { |
||
| 208 | e.preventDefault(); |
||
| 209 | var $this = $(this), |
||
| 210 | $block = $this.closest('#wpinv_checkout_cart_wrap'), |
||
| 211 | discount_code = $this.data('code'); |
||
| 212 | if (discount_code == '') { |
||
| 213 | return false; |
||
| 214 | } |
||
| 215 | var data = { |
||
| 216 | action: 'wpinv_remove_discount', |
||
| 217 | code: discount_code, |
||
| 218 | _nonce: WPInv.nonce |
||
| 219 | }; |
||
| 220 | wpinvBlock($block); |
||
| 221 | $.ajax({ |
||
| 222 | type: "POST", |
||
| 223 | data: data, |
||
| 224 | dataType: "json", |
||
| 225 | url: WPInv.ajax_url, |
||
| 226 | xhrFields: { |
||
| 227 | withCredentials: true |
||
| 228 | }, |
||
| 229 | success: function(res) { |
||
| 230 | if (res && typeof res == 'object') { |
||
| 231 | if (res.success) { |
||
| 232 | jQuery('#wpinv_checkout_cart_form', $this.checkout_form).replaceWith(res.data.html); |
||
| 233 | jQuery('.wpinv-chdeckout-total').text(res.data.total); |
||
| 234 | if (res.data.free) { |
||
| 235 | $('#wpinv_payment_mode_select', $this.checkout_form).hide(); |
||
| 236 | gw = 'manual'; |
||
| 237 | } else { |
||
| 238 | $('#wpinv_payment_mode_select', $this.checkout_form).show(); |
||
| 239 | gw = $('#wpinv_payment_mode_select', $this.checkout_form).attr('data-gateway'); |
||
| 240 | } |
||
| 241 | $('input[name="wpi-gateway"][value="' + gw + '"]', $this.checkout_form).prop('checked', true); |
||
| 242 | wpinvSetPaymentBtnText($('input[name="wpi-gateway"][value="' + gw + '"]', $this.checkout_form), res.data.free); |
||
| 243 | //console.log('291 : wpinv_recalculate_taxes()'); |
||
| 244 | //wpinv_recalculate_taxes(); |
||
| 245 | $(document.body).trigger('wpinv_discount_removed', [res]); |
||
| 246 | } |
||
| 247 | } |
||
| 248 | } |
||
| 249 | }).fail(function(res) { |
||
| 250 | wpinvUnblock($block); |
||
| 251 | if (window.console && window.console.log) { |
||
| 252 | console.log(res); |
||
| 253 | } |
||
| 254 | }); |
||
| 255 | return false; |
||
| 256 | }, |
||
| 257 | recalculate_taxes: function() { |
||
| 258 | console.log('308 : wpinv_recalculate_taxes()'); |
||
| 259 | wpinv_recalculate_taxes(); |
||
| 260 | } |
||
| 261 | } |
||
| 262 | WPInv_Checkout.init(); |
||
| 263 | }); |
||
| 264 | |||
| 351 | } |
This checks looks for references to variables that have not been declared. This is most likey a typographical error or a variable has been renamed.
To learn more about declaring variables in Javascript, see the MDN.